import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/operation/rb_enum.dart';
import 'package:reabampos/src/reabam/ui/pagestate_main_webview.dart';
import 'package:reabampos/src/reabam/ui/setting/pagestate_custom_reset_h5weburl.dart';
import 'package:reabampos/src/reabam/ui/support/support_control_devices.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_login.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';
import 'package:xsdk_flutter_plugin/xsdk_flutter_plugin.dart';

/*
欢迎页
hyl 2022.12.28
 */
class WelcomeStatePage extends XPageWelcomeState {
  WelcomeStatePage({required super.milliseconds});

  @override
  void onStart() {
    if (RBConfigs.versionType == VersionType.officialSelfServiceVersion ||
        RBConfigs.versionType == VersionType.testSelfServiceVersion) {
      //自助收银,竖屏
      XLogUtils.printLog('---自助收银欢迎界面');
      XFlutterUtils.setScreenOrientation(true);
    } else {
      //睿本云HD, 横屏
      XFlutterUtils.setScreenOrientation(false);
    }

    super.onStart();
    //
    if (XTempData.deviceInfo?['model'] != 'MT-S4Sp') {
      XFlutterUtils.setSystemStatusBar(false);
    }

    //保持设备唤醒状态
    XFlutterUtils.keepWakelock();

    //获得权限
    XPermissionUtils.requestAllPermissions(callback: (res) {
      //申请安装权限
      if (XFlutterUtils.isAndroid()) {
        XFlutterPluginUtils.checkFOREGROUND_SERVICEPermission();
        XFlutterPluginUtils.checkInstallPermission();
      } else {
        XPermissionUtils.requestPermission(Permission.requestInstallPackages);
      }
    });
  }

  @override
  void goNextPage() {
    if (RBConfigs.versionType == VersionType.officialSelfServiceVersion ||
        RBConfigs.versionType == VersionType.testSelfServiceVersion) {
      //自助收银
      startPage(StatePageSelfServiceLogin(), isFinish: true);
    } else {
      //睿本云HD
      startPage(
          MainWebViewStatePage(
            isUseWebview_flutter_4_10_0: XFlutterUtils.isMacOS(),
            isOpenCustomResetWebUrlPage: isOpenCustomResetWebUrlPage,
          ),
          isFinish: true);
    }

    //startPage(TestAndroidStatePage(),isFinish: true);
    //startPage(SettingStatePage(),isFinish: true);
    //startPage(TestRealmStatePage(),isFinish: true);
    //startPage(TestClientSocketStatePage(),isFinish: true);
  }

  @override
  Widget getWelcomeWidget() {
    if (XFlutterUtils.isiOS()) {
      return XWidgetUtils.getLayoutCenter(XWidgetUtils.getWidgetText('启动中...',
          style: XStyleUtils.textStyle_333_18()));
    } else {
      if (RBConfigs.versionType == VersionType.baodaoVersion) {
        //包道正式版
        return XWidgetUtils.getLayoutCenter(XWidgetUtils.getWidgetText('启动中...',
            style: XStyleUtils.textStyle_333_14()));
      } else {
        if (RBConfigs.versionType == VersionType.officialSelfServiceVersion ||
            RBConfigs.versionType == VersionType.testSelfServiceVersion) {
          //自助收银
          return XWidgetUtils.getLayoutCenter(
            XImageUtils.getImageWidgetWithAsset('reabam_logo_5.png',
                width: 360, height: 93),
          );
        } else {
          //睿本云HD
          return XWidgetUtils.getLayoutCenter(
              XWidgetUtils.getWidgetGestureDetector(
                  child: XImageUtils.getImageWidgetWithAsset('welcome_app.png',
                      width: 122, height: 32.5),
                  onDoubleTap: () {
                    isOpenCustomResetWebUrlPage = true;
                  }));
        }
      }
    }
  }

  bool isOpenCustomResetWebUrlPage = false;
}
